//
//  ServiceMercVC.swift
//  kxf
//
//  Created by Mac on 2024/3/10.
//

import Foundation
import MCToast

class ServiceMercVC: WHBaseViewVC {
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        initUI()
        sendUserCenterRequest()
        NotificationCenter.default.addObserver(self, selector: #selector(refreshUI), name: NSNotification.Name(rawValue: "refreshUserInfo"), object: nil)
    }
    lazy var topVm : ServiceMercTopVM = {
        let vm = ServiceMercTopVM.init(frame: .zero)
        
        return vm
    }()
    lazy var funcVm : ServiceMercFuncVM = {
        let vm = ServiceMercFuncVM.init(frame: .zero)
        vm.authenVM.tapBlock = {()in
//            if UserModel.shared.verifyStatusInt >= 1{
//                MCToast.mc_text("已完成实名")
//            }else{
                let vc = AuthenVC()
                self.navigationController?.pushViewController(vc, animated: true)
//            }
        }
        vm.rebateVm.tapBlock = {()in
            let vc = RebateVC()
            self.navigationController?.pushViewController(vc, animated: true)
        }
        vm.inviteCodeVm.tapBlock = {()in
//            let vc = InviteVC()
//            self.navigationController?.pushViewController(vc, animated: true)
        }
        vm.bankCardVm.tapBlock = {()in
//            if UserModel.shared.verifyStatusInt >= 1{
                let vc = BankCardVC()
                vc.bankType = "agent"
                self.navigationController?.pushViewController(vc, animated: true)
//            }else{
//                MCToast.mc_text("请先完成实名认证")
//            }
        }
        vm.serviceMercFeeVm.tapBlock = {()in
            let vc = SetRateVC()
            vc.isAgent = true
            vc.phone = UserModel.shared.phone
            vc.nextBtn.isHidden = true
            vc.isFromCenterTabbar = true
            self.navigationController?.pushViewController(vc, animated: true)
        }
//        vm.updatePswVm.tapBlock = {()in
//            let vc = UpdatePSWVC()
//            vc.isForget = false
//            self.navigationController?.pushViewController(vc, animated: true)
//        }
        vm.logoutVm.tapBlock = {()in
            self.logoutAction()
        }
        vm.settingVm.tapBlock = {()in
            let vc = SettingVC()
            self.navigationController?.pushViewController(vc, animated: true)
        }
        return vm
    }()
}

extension ServiceMercVC{
    func initUI() {
        view.addSubview(topVm)
        view.addSubview(scrollView)
        scrollView.addSubview(funcVm)
        
        view.backgroundColor = .COLOR_LIGHT_GREY
        scrollView.frame = CGRect.init(x: 0, y: self.topVm.frame.maxY+kFitWidth(10), width: SCREEN_WIDHT, height: SCREEN_HEIGHT-self.topVm.frame.maxY-kFitWidth(10)-getTabbarHeight())
        scrollView.contentSize = CGSize.init(width: 0, height: funcVm.frame.size.height+kFitWidth(20))
    }
    @objc func refreshUI() {
        topVm.updateUI()
        funcVm.inviteCodeVm.rightLabel.text = "\(UserModel.shared.inviteCode)"
        
        if UserModel.shared.verifyStatusInt >= 1{
            funcVm.authenVM.rightLabel.text = "已实名"
        }else{
            funcVm.authenVM.rightLabel.text = "未实名"
        }
    }
}

extension ServiceMercVC{
    @objc func logoutAction(){
        self.presentAlertVc(confirmBtn: "退出登录", message: "是否退出当前账号？", title: "温馨提示", cancelBtn: "取消", handler: { action in
            UserDefaults.standard.setValue("", forKey: whIdentity)
            UserDefaults.standard.setValue("", forKey: whTokenHead)
            UserDefaults.standard.setValue("", forKey: whToken)
            UserDefaults.standard.setValue("", forKey: Authorization)
            
            let appDelegate = UIApplication.shared.delegate as! AppDelegate
            let newRootVC = LoginVC()
            appDelegate.switchRootViewController(to: newRootVC)
            
        }, viewController: self)
    }
}
